import Ember from 'ember';
import BaseBusiness from '../base-business';
import Pagination from '../pagination';

export default BaseBusiness.extend(Pagination,{
  dataService: Ember.inject.service("date-service"),
  header_title:'执行情况列表',
  bedTypeID:'',
  model() {
    return {};
  },
  buildQueryParams:function(){
    var params=this.pagiParamsSet();
    var curController = this.getCurrentController();
    var filter={};
    var sort;

    if(curController){
      if(curController.get('customer')){
        filter = $.extend({}, filter, {'[plan][customer][name]':curController.get('customer.cname')});
        console.log('search name:',curController.get('customer.name'));
      }
      if(curController.get('staff')){
        filter = $.extend({}, filter, {'[exeStaff][name]':curController.get('staff.name')});
      }
      //时间搜索
      var queryCondition = curController.get('queryCondition');
      if (queryCondition) {
          var compareDate = null;
          if (queryCondition === "today") {
              compareDate = this.get("dataService").getTodayTimestamp();
          }
          if (queryCondition === "seven") {
              compareDate = this.get("dataService").getDaysBeforeTimestamp(7);
          }
          if (queryCondition === "thirty") {
              compareDate = this.get("dataService").getDaysBeforeTimestamp(30);
          }
          filter = $.extend({}, filter, {
              'exeStartTime@$gte': compareDate
          });
          console.log("compareDate is",compareDate);
      }
      //自定义日期搜索
      var conBeginDate = curController.get("beginDate");
      console.log("++++conBeginDate+++++",conBeginDate);
      var conEndDate = curController.get("endDate");
      console.log("++++conEndDate+++++",conEndDate);
      if (conBeginDate && conEndDate) {
          var beginCompare = this.get("dataService").getFirstSecondStampOfDay(conBeginDate);
          console.log("beginCompare is",beginCompare);
          var endCompare = this.get("dataService").getLastSecondStampOfDay(conEndDate);
          console.log("endCompare is",endCompare);
          filter = $.extend({}, filter, {
              'exeStartTime@$gte': beginCompare
          }, {
              'exeStartTime@$lte': endCompare
          });
      }
    }

    params.filter = filter;
    sort = {
      '[plan][customer][name]':'asc'
    };
    params.sort = sort;
    return params;
  },
  doQuery:function(){
    var params=this.buildQueryParams();
    var planExeList=this.findPaged('nursingplanexe',params,function(planExeList){});
    this.getCurrentController().set("planExeList", planExeList);
  },
  actions:{
    search(flag) {
        //alert("执行了");
        this.getCurrentController().set("queryCondition", flag);
        this.get("controller").set("beginDate", null);
        this.get("controller").set("endDate", null);
        this.doQuery();
    },
    //显示时间选择器
    showDate() {
        this.get("controller").set('dateShow', true);
    },
    //隐藏时间选择器
    hideDate() {
        this.get("controller").set('dateShow', false);
        this.doQuery();
    },
  },
  setupController: function(controller,model){

    this.doQuery();
    this.store.query('nursingplanexe',{}).then(function(planList){
      controller.set('planList',planList);
    });
    this._super(controller,model);
  }
});
